package com.example.work_cprovider2;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<Student> stuData = new ArrayList<>();
    private StuAdapter adapter;
    private Button addData;
    private LocalBroadcastManager localBroadcastManager;
    private MyBroadcastReceiver myBroadcastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        ContentResolver resolver = getContentResolver();
        listView = (ListView)findViewById(R.id.list_view);
        adapter = new StuAdapter(this,stuData,localBroadcastManager,resolver);
        listView.setAdapter(adapter);
        addData();
        readDatabese();
        register();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int a_id = stuData.get(position).getMunber();
                String a_name = stuData.get(position).getName();
                int a_age = stuData.get(position).getAge();
                Intent intent = new Intent(MainActivity.this,EditActivity.class);
                intent.putExtra("id",a_id);
                intent.putExtra("name",a_name);
                intent.putExtra("age",a_age);
                startActivity(intent);
            }
        });
    }
    private void addData(){
        addData = (Button)findViewById(R.id.add_student);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });
    }
    private void readDatabese(){
        Uri uri = Uri.parse("content://com.example.databasetext.provider/student");
        Cursor cursor = getContentResolver().query(uri,null,null,null,null);
        stuData.clear();
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            int number = cursor.getInt(0);
            String name =cursor.getString(1);
            int age = cursor.getInt(2);
            Student s = new Student(number,name,age);
            stuData.add(0,s);
        }
        cursor.close();
    }
    private void register(){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcast.ADD");
        myBroadcastReceiver = new MyBroadcastReceiver();
        localBroadcastManager.registerReceiver(myBroadcastReceiver,intentFilter);
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(myBroadcastReceiver);
    }
    class MyBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            readDatabese();
            adapter.notifyDataSetChanged();
        }
    }
}
